package com.zhouheng.springbootactiviti.common.utils;

import com.zhouheng.springbootactiviti.common.constants.ActivitiConstants;
import org.activiti.bpmn.model.EndEvent;
import org.activiti.bpmn.model.ExclusiveGateway;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.bpmn.model.StartEvent;
import org.activiti.bpmn.model.UserTask;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.List;

public class ActivitiUtils {

    private ActivitiUtils() {

    }

    public static StartEvent createStartEvent() {
        StartEvent startEvent = new StartEvent();
        startEvent.setId(ActivitiConstants.START);
        return startEvent;
    }

    public static EndEvent createEndEvent() {
        EndEvent endEvent = new EndEvent();
        endEvent.setId(ActivitiConstants.END);
        return endEvent;
    }

    public static UserTask createUserTask(String id, String name, String assignee, List<String> candidateUsers, List<String> candidateGroups) {
        UserTask userTask = new UserTask();
        userTask.setName(name);
        userTask.setId(id);
        // 设置代理人
        if (StringUtils.isNotEmpty(assignee)) {
            userTask.setAssignee(assignee);
        }
        if (!CollectionUtils.isEmpty(candidateUsers)) {
            userTask.setCandidateUsers(candidateUsers);
        }
        if (!CollectionUtils.isEmpty(candidateGroups)) {
            userTask.setCandidateGroups(candidateGroups);
        }
        return userTask;
    }

    public static UserTask createUserTask(String id, String name, List<String> candidateGroups) {
        return createUserTask(id, name, null, null, candidateGroups);
    }

    public static ExclusiveGateway createExclusiveGateway(String id) {
        ExclusiveGateway exclusiveGateway = new ExclusiveGateway();
        exclusiveGateway.setId(id);
        return exclusiveGateway;
    }

    public static SequenceFlow createSequenceFlow(String from, String to, String name, String conditionExpression) {
        SequenceFlow flow = new SequenceFlow();
        flow.setSourceRef(from);
        flow.setTargetRef(to);
        flow.setName(name);
        // 设置条件表达式
        if (StringUtils.isNotEmpty(conditionExpression)) {
            flow.setConditionExpression(conditionExpression);
        }
        return flow;
    }

}
